package org.openzip.openzip.archiving.gui;

import org.openzip.jarsfx.IArchiver;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import org.openzip.openzip.gui.util.ProgressWindow;
import org.openzip.openzip.gui.util.SkeletonGUI;


/**
 * This is an implementation of <code>IArchiverUI</code> interface.<br>
 * It provides a graphical user interface for the archiving logic. For seeing
 * how the GUI looks like, refer to the specification.<br>
 * <br>
 * This class subclasses SkeletonGUI to provide its functionality.
 * 
 * @author Jakub Závěrka
 */
public class ArchiverGUI extends SkeletonGUI  implements IArchiverUI
{
	private IArchiver logic;
	
	public ArchiverGUI(){
		super("OpenZIP Archiver BETA", "Make Archive", new ArchiverFilePanel(), 
					new TreeRootFile(), true);
		okButton.addActionListener(new MakeArchiveListener());
		System.out.println("SkeletonGUI created");
	}
	
	/**
	 * {@inheritDoc} <br>
	 * This implementaion will display an empty environment.
	 */
	@Override
	public void useUI(IArchiver logic) {
		this.logic = logic;
		mainWindow.setVisible(true);
	}

	/**
	 * {@inheritDoc} <br>
	 * This implementaion will display the environment with specified files
	 * pre-selected. Changes will be reflected in the tree, folder list and file
	 * list. Other files may get selected and the pre-selected files amy get
	 * de-selected.
	 * 
	 * This method is currently not supported.
	 */
	@Override
	public void useUI(IArchiver logic, Collection<File> files) {
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	

	private class MakeArchiveListener implements ActionListener 
	{
		/**
		 * Validates the specified archive file and calls the <code>makeArchive
		 * </code> method if all validations pass.
		 */
		@Override
		public void actionPerformed(ActionEvent event) {
			File destination = filePanel.getFile();
			//validation whether the file exists
			if(!destination.exists()){
				//if no, try to create new
				try{
					destination.createNewFile();
				}
				catch(IOException e){
					//if the creation fails, display a message and return.
					JOptionPane.showMessageDialog(mainWindow, 
								"You do not have write permission on provided filepath. " +
								"Select another file on another filepath.", "Warning", 
								JOptionPane.WARNING_MESSAGE);
				}
			}
			//destination exists, test if we can write
			if(!destination.canWrite()){
				//if no, display a message and return.
				JOptionPane.showMessageDialog(mainWindow, 
							"You do not have write permission on provided file. " +
							"Please select another file.", "Warning", 
							JOptionPane.WARNING_MESSAGE);
				return;
			}
			try{
				//all validations passed, we may begin making the archive.
				ProgressWindow progressWindow = new ProgressWindow();
				progressWindow.setLocationRelativeTo(mainWindow);
				progressWindow.setVisible(true);
				logic.setRefreshablePacker(progressWindow);
				new Thread(new Worker()).start();

			}
			catch(Exception ex){
				JOptionPane.showMessageDialog(mainWindow, 
							"An error occured during creating the archive.", "Warning", 
							JOptionPane.WARNING_MESSAGE);
				ex.printStackTrace();
			}
		}
	}
	
	private class Worker implements Runnable
	{
		@Override public void run(){
			try{
				logic.makeArchive(filePanel.getFile(), globalSelectionModel
					.getSelectedFiles());
			}
			catch(IOException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
